<template>
	<view>
		<view>
			<table-title content="停车位申请表详情"></table-title>
			<view class="record-id">单号：{{processNo}}</view>
			<view class="in-center">
				<small-title content="基本资料"></small-title>
				<view class="state-bar">
					<view class="state-msg">{{state}}</view>
				</view>
			</view>
			<view class="item-table">
				<view-item name="租户名称" :value="userMsg.userCompany"></view-item>
				<view-item name="楼盘" :value="userMsg.userBuilding"></view-item>
				<view-item name="楼层" :value="userMsg.userFloor"></view-item>
				<view-item name="房间号" :value="userMsg.userRoom" :isLast="true"></view-item>
			</view>
			<small-title content="车位类型"></small-title>
			<view style="margin-bottom: 20rpx;">
				<radio-group :disabled="!(status===-1)" class="radiogroup-style">
					<view @click="chooseNormal(true)" class="first-radio">
						<radio :disabled="!(status===-1)" :checked="normalParking && (!otherParking)" color="#FF0000"></radio>
						<view >普通车位</view>
					</view>
					<view  @click="chooseNormal(false)" class="second-radio">
						<radio :disabled="!(status===-1)" :checked="!normalParking && (!otherParking)" color="#FF0000"></radio>
						<view>子母车位</view>
					</view>
					<view  @click="chooseOther(true)" class="second-radio">
						<radio :disabled="!(status === -1)" :checked="otherParking" color="#FF0000"></radio>
						<view>其它</view>
					</view>
				</radio-group>
			</view>
			<view v-show="otherParking">
				<small-title :isNeed="true" content="办理备注"></small-title>
				<textarea class="textarea-content-style" :disabled="true" v-model="otherContent"></textarea>
			</view>
			<u-divider bg-color="#FFFFFF">车位办理信息一</u-divider>
			<small-title content="车主姓名"></small-title>
			<textarea class="textarea-name-style" maxlength="-1" :disabled="!(status === -1)" :value="userList[0].userName" @input="inputNameDetialFirst"></textarea>
			<small-title content="车牌号"></small-title>
			<textarea class="textarea-name-style" maxlength="-1" :disabled="!(status === -1)" :value="userList[0].carNumber" @input="inputParkNumDetialFirst"></textarea>
			<small-title content="上传材料"></small-title>
			<park-card-img :firstImg="userList[0].imageList[0]" :secondImg="userList[0].imageList[1]" 
			:thirdImg="userList[0].imageList[2]" :fourthImg="userList[0].imageList[3]" :fifthImg="userList[0].imageList[4]" 
			:sixthImg="userList[0].imageList[5]" :otherImg="userList[0].imageList[6]" :key="1" :isChange="state==='待申请' || state==='已驳回'" v-on:addImg="addImgFirst"></park-card-img>
			
			<view v-if="!normalParking && (!otherParking)">
				<u-divider bg-color="#FFFFFF">车位办理信息二</u-divider>
				<small-title content="车主姓名"></small-title>
				<textarea class="textarea-name-style" maxlength="-1" :disabled="!(status === -1)" :value="userList[1].userName" @input="inputNameDetialSecond"></textarea>
				<small-title content="车牌号"></small-title>
				<textarea class="textarea-name-style" maxlength="-1" :disabled="!(status === -1)" :value="userList[1].carNumber" @input="inputParkNumDetialSecond"></textarea>
				<small-title content="上传材料"></small-title>
				<park-card-img :firstImg="userList[1].imageList[0]" :secondImg="userList[1].imageList[1]" 
				:thirdImg="userList[1].imageList[2]" :fourthImg="userList[1].imageList[3]" :fifthImg="userList[1].imageList[4]" 
				:sixthImg="userList[1].imageList[5]" :otherImg="userList[0].imageList[6]" :key="1" :isChange="state==='待申请' || state==='已驳回'" v-on:addImg="addImgSecond"></park-card-img>
			</view>
			
			<view v-if="!otherParking" class="small-title">办理费用：{{spend}} 元</view>
			<view v-if="status > 2 && status != 4">
				<view class="small-title">支付信息截图</view>
				<image class="pay-image" :src="chargeImg" mode="scaleToFill" @tap='chooseImage'></image>
			</view>
			<view v-if="status > 0 && status != 4">
				<small-title :isNeed="true" content="车位租用协议"></small-title>
				<view class="download-text" @click="downloadFile(0)">{{ docxFileName }}</view>
				<button v-if="status===1 && userRightDetail.goodsRelease.right" class="add-button" @click="uploadFile(0)">上传文件</button>
			</view>
			<small-title content="处理进度"></small-title>
			<proccess-bar v-if="processFlag" :proccessList="proccessListDefult"></proccess-bar>
			<view v-if="status===0" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="verifyOrder(0)">审核通过</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="open">驳回申请</button>
				</view>
			</view>
			<view v-if="status===1" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="confirmDelivery" >上传协议</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view v-if="status===3" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="bbFinish" >确认已办结</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog mode="input" message="成功消息" title="请输入驳回理由" :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<l-file
		    ref="lFile" 
			@up-success="upSuccess"
		></l-file>
	</view>
</template>

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE_2,
		myUploadFile,
		myDownloadFile,
		syncGetOrder
	} from "../../../util/network.js"
	import {
		processHistoryList
	} from "../../../util/common.js"
	export default{
		data() {
			return {
				userRightDetail:'',
				processFlag:false,
				token:'',
				recordId: '待分配',
				processNo:'待分配',
				state: '待受理',
				status: -1,
				userMsg:{'userName':'','userPhone':'',
					'userCompany':'','userBuilding':''},
				normalParking:true,
				otherParking:false,
				spend:0,
				parkCardPrice:{'normal':-1,'vip':-1,'double':-1},
				userList:[
					{'userName':'',
					'parkType':0,
					'carNumber':'',
					'imageList':['/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								0,0,0,0,0,0,0
								]
					},
					{'userName':'',
					'parkType':0,
					'carNumber':'',
					'imageList':['/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								'/static/uploadImg.png',
								0,0,0,0,0,0,0
								]
					}
				],
				chargeImg:'/static/uploadImg.png',
				chargeImgId:-1,                                     //支付信息截图
				nopassReasons:"楼层管家审核通过",
				proccessList:[
					{'type':'firstCanvas','commentMsg':'发起申请','time':'2021-01-01 01:01:11'},
					{'type':'middleCanvas','commentMsg':'制卡完毕','time':'2020-02-02 02:02:22'},
					{'type':'lastCanvas','commentMsg':'收到卡片和票据','time':'2020-03-03 03:33:33'}
				],
				proccessListDefult:[],
				cardPrice:0,
				previewUrl:ONLINE_SERVER_ADDRESS_BASE_2 + '/file/preview/',
				docxFileId:0,
				docxFileName:'车位租用协议.docx',
				otherContent:'',
			}
		},
		methods: {
			downloadFile(fileType,index){
				let fileId = 0
				if(fileType == 0){
					fileId = this.docxFileId
				}
				if(!(parseInt(fileId) > 0)){
					uni.showModal({
						content:'找不到该文件,请检查是否已上传。',
						showCancel:false
					})
					return 0
				}
				this.$refs.lFile.download({
					url:`${ONLINE_SERVER_ADDRESS_BASE_2}/file/download/` + fileId,
					token:this.token,
				})
				.then(path=>{
				    // 打开文件
				    this.$refs.lFile.open(path);
				});
			},
			upSuccess(e){
				console.log(e)
				if(this.fileType == 0){
					this.docxFileName = e.fileName
					this.docxFileId = e.data
				}
			},
			
			uploadFile(status){
				this.fileType = status
				this.$refs.lFile.upload({
				    // #ifdef APP-PLUS
				    //（app端必传）nvue页面使用时请查阅nvue获取当前webview的api，当前示例为vue窗口
				    currentWebview: this.$mp.page.$getAppWebview(),
				    // #endif
				    //替换为你的上传接口地址
				    url: `${ONLINE_SERVER_ADDRESS_BASE_2}/file/upload`,
				    // 服务端接收附件的key
				    name: 'file',
				    //根据你接口需求自定义 (优先不传content-type,安卓端无法收到参数再传)
				    header: {
						'Authorization': this.token,
				        // 'Authorization': 'bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6eyJ1c2VyTmFtZSI6',
				        // 'uid': '27682',
				        // 'client': 'app',
				        // 'accountid': 'DPOA9487'
				    },
				
				    // 限制选择附件的大小上限，默认10M
				    // maxSize: 20,
				
				    // 若需要在body单独添加附件名或附件大小如下方式传入组件：
				    // addName: '后端要的附件名称字段key,此处请勿写name的同值如(file)，会覆盖name',
				    // addSize: '后端要的附件大小字段key'
				
				    // body参数直接写key,value,如：
				    // date: '2020-1-1',
				    // key2: 'value2',
				});
			},
			open() {
				this.$refs.popup.open()
			},
			/**
			 * 点击取消按钮触发
			 * @param {Object} done
			 */
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			/**
			 * 点击确认按钮触发
			 * @param {Object} done
			 * @param {Object} value
			 */
			confirm(value) {
				this.$refs.popup.close()
				if(value == ""){
					uni.showModal({
						content:'理由不能为空',
						showCancel:false
					});
					this.$refs.popup.close()
				}else{
					if(this.userRightDetail.knotCommission.primeAuth == 1){
						this.nopassReasons = "运营主管已驳回，原因：" + value
					}else{
						this.nopassReasons = "楼层管家已驳回，原因：" + value
					}
					this.verifyOrder(1)
					// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
					// ...
					this.$refs.popup.close()
				}
			},
			inputNameDetial(e){
				this.userList[0].userName = e.detail.value
			},
			
			inputParkNumDetial(e){
				this.userList[0].carNumber = e.detail.value
			},
			
			chooseNormal(flag){
				if(this.status == -1){
					this.normalParking = flag
					if(flag){
						this.userList[0].parkType = 0
						this.userList[1].parkType = 0
						this.spend = this.parkCardPrice.vip
					}else{
						this.userList[0].parkType = 2
						this.userList[1].parkType = 2
						this.spend = this.parkCardPrice.double
					}
				}
			},
			
			/*
			功能：子组件事件触发修改对应行的停车位办理信息
			参数：
			imageList：照片信息列表
			*/
			addImg(imageList){
				console.log("[info]","调试父子组件通信，我是父组件" + imageList)
				var strs= new Array();
				strs=imageList.split(",");
				for(var i = 0; i < 6; i++){
					console.log("[info]","测试停车照片数组" + strs[i])
					if(strs[i]!=''){
						this.userList[0].imageList[i]=strs[i]
						this.userList[0].imageList[i + 6]=parseInt(strs[i + 6])
					}
				}
				this.$forceUpdate()
			},
			
			/*
			功能：支付截图点击事件触发修改或预览照片，如果是待申请 则点击修改照片，否则则是预览
			参数：无
			*/
			chooseImage(){
				uni.previewImage({
					current: 0,
					urls: [this.chargeImg]
				})
			},
			
			async loadImg(chargeFileId){
				// let _this = this
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE_2}/file/download/` + chargeFileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			
			confirmDelivery(){
				console.log("[info]","楼层管家已派送：" + this.recordId)
				if(!(this.docxFileId > 0)){
					uni.showModal({
						content:'请上传车位租用协议',
						showCancel:false
					})
					return 0
				}
				if(this.userRightDetail.knotCommission.primeAuth == 1){
					this.nopassReasons = "运营主管已上传"
				}else{
					this.nopassReasons = "楼层管家已上传"
				}
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE_2}/parking-space/confirm/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						fileId: this.docxFileId,
						commentMsg: this.nopassReasons                       //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("楼层管家已派送成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("楼层管家已派送调用失败")
					}
				})
			},
			
			bbFinish(){
				console.log("[info]","楼层管家已派送：" + this.recordId)

				if(this.userRightDetail.knotCommission.primeAuth == 1){
					this.nopassReasons = "运营主管已确认办结"
				}else{
					this.nopassReasons = "楼层管家已确认办结"
				}
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE_2}/parking-space/bb-finish/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						commentMsg: this.nopassReasons                       //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("楼层管家已派送成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("楼层管家已派送调用失败")
					}
				})
			},
			
			verifyOrder(status){
				console.log("[info]","楼层管家审核：" + this.recordId)
				if(status != 1){
					if(this.userRightDetail.knotCommission.primeAuth == 1){
						this.nopassReasons = "运营主管审核通过"
					}else{
						this.nopassReasons = "楼层管家审核通过"
					}
				}
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE_2}/parking-space/verify/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						nopassReasons:this.nopassReasons,
						verifyResult:status,
						commentMsg: this.nopassReasons,                      //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("楼层管家审核成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("楼层管家审核调用失败")
					}
				})
			},
			getOrder(){
				let a = syncGetOrder("parking-space/show/" + this.recordId)
				 a.then((res)=>{
					 console.log("异步",res)
					 if(res.data.code == 200){
						 this.status = parseInt(res.data.data.status)
						 switch(this.status) {
							  case 0:
								 this.state = "待管家审核"
								 break;
							  case 1:
								 this.state = "待管家上传"
								 break;
							 case 2:
								 this.state = "待用户上传"
								break;
							 case 3:
								 this.state = "待管家办结"
								break;
							 case 4:
								 this.state = "已驳回申请"
								break;
							case 5:
								this.state = "已办结"
							   break;
						    default:
						    this.status = -1
						       this.state = "待申请"
						 } 
						 this.userMsg.userCompany = res.data.data.createUserInfo.name
						 this.userMsg.userBuilding = res.data.data.createUserInfo.title
						 this.userMsg.userFloor = res.data.data.createUserInfo.floor
						 this.userMsg.userRoom = res.data.data.createUserInfo.houseNumber
						 this.userList = JSON.parse(res.data.data.detail)
						 this.normalParking = (JSON.parse(res.data.data.detail)[0].parkType == 0 || JSON.parse(res.data.data.detail)[0].parkType == -1)
						 this.otherParking = (JSON.parse(res.data.data.detail)[0].parkType == -1)
						 if(this.otherParking){
						 	this.otherContent = res.data.data.otherContent
						 }
						 if(this.normalParking){
							 this.userList.splice(1,1)
						 }
						 this.docxFileId = res.data.data.fileId
						 this.processNo = res.data.data.processNo
						 this.spend = res.data.data.price
						 this.chargeImgId = res.data.data.payScreenshotId
						 this.proccessListDefult=processHistoryList(res.data.data.processDetail)
						 this.processFlag = true
						 let b = this.loadImg(this.chargeImgId)
						 b.then((v)=>{
						 	this.chargeImg = v
						 })
						for (var i = 0; i < this.userList.length; i++) {
							let index = i
							for (var j = 0; j < 7; j++) {
								if(parseInt(this.userList[index].imageList[j + 7]) > 0){
									this.userList[index].imageList[j] = this.previewUrl + this.userList[index].imageList[j + 7]
								}else{
									this.userList[index].imageList[j] = this.userList[index].imageList[j + 7]
								}
							}
						}

						this.$forceUpdate()
					}
				})
			},
			back(){
				uni.navigateBack()
			},
		},

		onLoad(option) {
			//传入单号
			if(uni.getStorageSync('userinfo') != null){
				this.userRightDetail = uni.getStorageSync("userRight")
				this.recordId = option.id
				// this.state = option.state
				this.token = uni.getStorageSync('token')
				console.log(this.token)
			}else{
				uni.navigateTo('../../login/login')
			}
		},
		mounted() {
			//如果是新建表单则不查询
			if(this.state != '待申请'){
				this.getOrder()
			}
		}
	}
</script>

<style>
/* 	@import "../../../static/css/upload-imgs.css"; */
	page{
		background-color: #ffffff
	}
	.record-id{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.state-bar{
		align-items: center;
		color: #FFFFFF;
		background-color: #DD524D;
		width: 260rpx;
		justify-content: flex-end;
		margin-left: auto;
		margin-right: 50rpx;
		font-size: 30rpx;
		border-radius: 50rpx;
		height: 50rpx;
	}
	.state-msg{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.in-center{
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.item-table{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.picture-part{
		display: flex;
		margin: 0 0 10rpx 50rpx;
		font-size: 100rpx;
		font-weight: 200;
		align-items: center;
		justify-content: center;	
		width: 150rpx;
		height: 150rpx;
		border: dashed 1rpx #000000;
	}
	.picture-style{
		width: 155rpx;
		height: 155rpx;
		margin: 0 0 10rpx 50rpx;
	}
	.picture-note-access{
		display: flex;
		justify-content: center;
		align-items: center;
		margin-left: 50rpx;
		font-size: 30rpx;
	}
	.picture-block-access{
		width: 201rpx;
		display: flex;
		flex-direction: column;
	}
	.picture-block{
		display: flex;
		flex-direction: column;
		justify-content: center;
		margin-left: 60rpx;
	}
	.mix-button{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.left-part{
		float: right;
	}
	.right-part{
		float: left;
	}
	.left-button{
		background-color: #FF0000;
		border-radius: 50rpx 0 0 50rpx;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.right-button{
		background-color: #0000FF;
		border-radius: 0 50rpx 50rpx 0;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.right-button[disabled]{
		background-color: #555555;
		border-radius: 0 50rpx 50rpx 0;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.picture-list{
		display: flex;
		margin-top: 10rpx;
	}
	.wrong-logo{
		width: 40rpx;
		height: 40rpx;
		margin-bottom: -40rpx;
		margin-left: 165rpx;
		z-index: 999;
		background-color: #555555
	}
	.add-button {
		border-width: 0px;
		align-items: center;
		justify-content: center;
		margin: 30rpx 0 40rpx 250rpx;
		height: 50rpx;
		width: 250rpx;
		display: flex;
		color: #22B242;
	}
	.cost-all{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.small-title{
		margin: 20rpx 0 20rpx 50rpx;
	}
	.pay-image{
		border:2rpx solid;
		border-radius: 2px;
		margin: 0rpx 0 0rpx 80rpx;
		width: 126rpx;
		height: 126rpx;
	}
	.textarea-name-style{
		display: flex;
		justify-content: center;
		width: 650rpx;
		height: 75rpx;
		font-size: 55rpx;
		margin-left: 50rpx;
		background-color: #FFFFFF;
		border: solid 1rpx #C0C0C0;
	}
	.textarea-content-style{
		display: flex;
		justify-content: center;
		width: 650rpx;
		height: 150rpx;
		font-size: 28rpx;
		margin-left: 50rpx;
		background-color: #FFFFFF;
		border: solid 1rpx #C0C0C0;
	}
	.radiogroup-style{
		width: 650rpx;
		height: 100rpx;
		background-color: #FFFFFF;
		display: flex;
		margin-left: 50rpx;
		justify-content: center;
		border: solid 1rpx #C0C0C0;
	}
	.first-radio{
		display: flex;
		height: 100rpx;
		width: 180rpx;
		align-items: center;
	}
	.second-radio{
		display: flex;
		height: 100rpx;
		align-items: center;
		margin-left: 50rpx;
	}
	.download-text {
	  -webkit-box-sizing: content-box;
	  -moz-box-sizing: content-box;
	  box-sizing: content-box;
	  border: none;
	  font: normal 16px/1 "Times New Roman", Times, serif;
	  color: rgba(18,25,232,1);
	  text-decoration: underline;
	  -o-text-overflow: ellipsis;
	  text-overflow: ellipsis;
	  margin-left: 60rpx;
	  margin-bottom: 10rpx;
	}
</style>
